/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.service;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.MathUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ins.dao.InsAuxiliaryLineDao;
import com.thinkgem.jeesite.modules.ins.dao.InsDataLinkDao;
import com.thinkgem.jeesite.modules.ins.dao.InsQuotaDao;
import com.thinkgem.jeesite.modules.ins.dao.InsYaxisDao;
import com.thinkgem.jeesite.modules.ins.dao.InsTableDao;
import com.thinkgem.jeesite.modules.ins.entity.InsAuxiliaryLine;
import com.thinkgem.jeesite.modules.ins.entity.InsCount;
import com.thinkgem.jeesite.modules.ins.entity.InsQuota;
import com.thinkgem.jeesite.modules.ins.entity.InsYaxis;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
/**
 * 指标卡规则Service
 * @author fanqinglong
 * @version 2018-03-15
 */
@Service
@Transactional(readOnly = true)
public class InsQuotaService extends CrudService<InsQuotaDao, InsQuota> {
	@Autowired
	private InsYaxisDao insYaxisDao;
	@Autowired
	private InsAuxiliaryLineDao insAuxiliaryLineDao;
	@Autowired
	private InsDataLinkDao insDataLinkDao;
	@Autowired
	private InsTableDao insTableDao;
	
	public InsQuota get(String id) {
		InsQuota insQuota = dao.get(id);
		InsYaxis param = new InsYaxis();
		param.setQuota(insQuota);
		param.setType("1");
		List<InsYaxis> yaxisList = insYaxisDao.findList(param);
		insQuota.setYaxisList(yaxisList);
		param.setType("2");
		List<InsYaxis> minorList = insYaxisDao.findList(param);
		insQuota.setMinorList(minorList);
		InsAuxiliaryLine line = new InsAuxiliaryLine();
		line.setQuota(insQuota);
		line.setType("1");
		List<InsAuxiliaryLine> yauxiliaryList = insAuxiliaryLineDao.findList(line);
		insQuota.setYauxiliaryList(yauxiliaryList);
		line.setType("2");
		List<InsAuxiliaryLine> mauxiliaryList = insAuxiliaryLineDao.findList(line);
		insQuota.setMauxiliaryList(mauxiliaryList);
		return insQuota;
	}
	
	public List<InsQuota> findList(InsQuota insQuota) {
		List<InsQuota> list = super.findList(insQuota);
		for(InsQuota item : list)
		{
			InsTable table = insTableDao.get(item.getTable().getId());
			item.setTable(table);
		}
		return list;
	}
	
	public Page<InsQuota> findPage(Page<InsQuota> page, InsQuota insQuota) {
		return super.findPage(page, insQuota);
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void save(InsQuota insQuota) {
		super.save(insQuota);
		//删除该规则下所有数值和辅助线
		insYaxisDao.deleteByQuota(insQuota.getId());
		insAuxiliaryLineDao.deleteByQuota(insQuota.getId());
		//重新新增
		List<InsYaxis> yaxisList = insQuota.getYaxisList();
		List<InsYaxis> minorList = insQuota.getMinorList();
		List<InsAuxiliaryLine> yauxiliaryList = insQuota.getYauxiliaryList();
		List<InsAuxiliaryLine> mauxiliaryList = insQuota.getMauxiliaryList();
		//新增设定值
		if ("6".equals(insQuota.getYaxisType())) {
			InsAuxiliaryLine settingValue = insQuota.getSettingValue();
			settingValue.preInsert();
			settingValue.setType("3");
			settingValue.setQuota(insQuota);
			insAuxiliaryLineDao.insert(settingValue);
		}
		if (null != yaxisList) {
			for (InsYaxis insYaxis : yaxisList) {
				if(insYaxis.getDelFlag().equals("1"))
				{
					continue;
				}
				insYaxis.setQuota(insQuota);
				insYaxis.setType("1");
				insYaxis.preInsert();
				insYaxisDao.insert(insYaxis);
			}
		}
		if (null != minorList) {
			for (InsYaxis insYaxis : minorList) {
				if(insYaxis.getDelFlag().equals("1"))
				{
					continue;
				}
				insYaxis.setQuota(insQuota);
				insYaxis.setType("2");
				insYaxis.preInsert();
				insYaxisDao.insert(insYaxis);
			}
		}
		if(yauxiliaryList != null)
		{
			for(InsAuxiliaryLine auxiliaryLine : yauxiliaryList)
			{
				if(auxiliaryLine.getDelFlag().equals("1"))
				{
					continue;
				}
				auxiliaryLine.setType("1");
				auxiliaryLine.setQuota(insQuota);
				auxiliaryLine.preInsert();
				insAuxiliaryLineDao.insert(auxiliaryLine);
			}
		}
		if(mauxiliaryList != null)
		{
			for(InsAuxiliaryLine auxiliaryLine : mauxiliaryList)
			{
				if(auxiliaryLine.getDelFlag().equals("1"))
				{
					continue;
				}
				auxiliaryLine.setType("2");
				auxiliaryLine.setQuota(insQuota);
				auxiliaryLine.preInsert();
				insAuxiliaryLineDao.insert(auxiliaryLine);
			}
		}
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void delete(InsQuota insQuota) {
		super.delete(insQuota);
	}
	
	public double getResult(InsQuota insQuota){
		double result = 0;
		/*BigDecimal bigDecimal = new BigDecimal(0);
		if ("3".equals(insQuota.getType())) {
			bigDecimal = dao.getMedium(insQuota);
		}else {
			bigDecimal = dao.getResult(insQuota);
		}
		result = (bigDecimal == null)?0:bigDecimal.doubleValue();*/
		return result;
	}
	public List<InsQuota> findNotQuotaList(InsQuota insQuota){
		return dao.findNotQuotaList(insQuota);
	}
	
	public JSONObject getData(InsQuota insQuota){
		//如果需要展示指标卡，则不需要画图表，所以单独处理
		if ("7".equals(insQuota.getYaxisType())) {
			JSONObject jsonObject = new JSONObject();
			List<InsYaxis> yaxisList = insQuota.getYaxisList();
			//指标卡仅有一个数值
			for (InsYaxis yaxis : yaxisList) {
				if ("0".equals(yaxis.getDelFlag())) {
					String dateSQL = "c.`value`";
					List<InsCount> countList = dao.selectForCount(insQuota.getXaxis().getId(), yaxis.getField().getId(),yaxis.getAlgorithm(),insQuota.getDateType(),dateSQL,yaxis.getDigit());
					if (countList.size() > 0) {
						for (InsCount insCount : countList) {
							jsonObject.put("name", yaxis.getField().getName());
							jsonObject.put("value", insCount.getResult());
							jsonObject.put("quota", true);
						}
					}
				}
			}
			return jsonObject;
		}
		JSONObject option = new JSONObject();
		JSONObject title = new JSONObject();
		title.put("show", false);
		JSONObject tooltip = new JSONObject();
		//图例
		JSONObject legend = new JSONObject();
		//data需要放数组，多条数值的集合
		//遍历insQuota的数值集合，构建数组
		List<InsYaxis> yaxisList = insQuota.getYaxisList();
		List<InsYaxis> minorList = insQuota.getMinorList();
		//辅助线
		List<InsAuxiliaryLine> yauxiliaryLineList = insQuota.getYauxiliaryList();
		List<InsAuxiliaryLine> mauxiliaryLineList = insQuota.getMauxiliaryList();
		//X轴
		JSONObject xaxis = new JSONObject();
		String[] xaxisDataArray = null;
		//series
		JSONArray series = new JSONArray();
		//饼图专用series数组，特殊性：只含有一个object的数组
		JSONArray pieSeries = new JSONArray();
		//计量图专用series数组
		JSONArray gaugeSeries = new JSONArray();
		//无维度多数值情况饼图用的pieObject
		JSONObject pieObjectNoXaxis = new JSONObject();
		//有维度1数值情况饼图用pieObject
		JSONObject pieObject = new JSONObject();
		//无维度多数值情况饼图用的dataArray
		JSONArray pieDataArrayNoXasix = new JSONArray();
		//有维度1数值情况饼图用的pieDataArray
		JSONArray pieDataArray = new JSONArray();
		//计量图和指标卡专用object
		JSONObject gaugeObject = new JSONObject();
		//计量图和指标卡专用dataArray
		JSONArray gaugeDataArray = new JSONArray();
		//是否需要倾斜展示x轴，避免x轴显示不全
		boolean rotate = false;
		//最大值,用于设置y轴最大值计算
		double maxValue = 0.0;
		//数值处理逻辑
		if (null != yaxisList && yaxisList.size() > 0) {
			for (int i = 0; i < yaxisList.size(); i++) {
				InsYaxis yaxis = yaxisList.get(i);
				if ("0".equals(yaxis.getDelFlag())) {
					//非饼图用seriesObject
					JSONObject seriesObject = new JSONObject();
					//如果按日期分组的话，日期SQL，默认取字段，可以选择单据创建日期
					//默认直接拿c.value,当dateType有值的时候str_to_date,当xaxis==createDate时idd.create_date
					//createDate:xaxis==createDate&&dateType==''
					//str_to:dateType!=''
					String dateSQL = "c.`value`";
					if ("createDate".equals(insQuota.getXaxis().getId())) {
						dateSQL = "idd.create_date";
					}
					//选了年月日，已确定按date字段分组
					if (!"".equals(insQuota.getDateType()) && !"createDate".equals(insQuota.getXaxis().getId())) {
						dateSQL = "str_to_date(c.`value`,'%Y年%m月%d日 %H时%i分%s秒')";
					}
					List<InsCount> countList = dao.selectForCount(insQuota.getXaxis().getId(), yaxis.getField().getId(),yaxis.getAlgorithm(),insQuota.getDateType(),dateSQL,yaxis.getDigit());
					xaxisDataArray = new String[countList.size()];
					String[] seriesData = new String[countList.size()];
					for (int j = 0; j < countList.size(); j++) {
						InsCount insCount = countList.get(j);
						xaxisDataArray[j] = insCount.getXaxis();
						seriesData[j] = insCount.getResult();
						//最大值
						maxValue = (maxValue > Double.valueOf(insCount.getResult()))? maxValue:Double.valueOf(insCount.getResult());
						//如果选择了维度，且后面知道是饼图的话，肯定只有一个数值。这个二重循环只可能进一次，从这里拿值去交给饼图
						//如果不是饼图，这个serise不会使用
						if (null != insQuota.getXaxis() && !StringUtils.isEmpty(insQuota.getXaxis().getId())) {
							JSONObject pieData = new JSONObject();
							pieData.put("name", insCount.getXaxis());
							pieData.put("value", insCount.getResult());
							pieDataArray.put(pieData);
						}else {
							//无维度的情况下，x轴无意义
							xaxisDataArray[j] = "统计结果";
							//无维度的情况下在此设定pieObjectNoXaxis的值
							//这种情况下每个数值的循环体内，只可能有一个pieObject
							JSONObject pieDataNoXaxis = new JSONObject();
							pieDataNoXaxis.put("name", yaxis.getField().getName());
							pieDataNoXaxis.put("value", insCount.getResult());
							pieDataArrayNoXasix.put(pieDataNoXaxis);
							//计量图和指标卡没有维度，逻辑和饼图一样
							JSONObject gaugeData = new JSONObject();
							gaugeObject.put("name", yaxis.getField().getName());
							gaugeObject.put("max", insCount.getResult());
							gaugeData.put("name", yaxis.getField().getName());
							gaugeData.put("value", insCount.getResult());
							gaugeDataArray.put(gaugeData);
						}
						rotate = xaxisDataArray[j].length()>10 || countList.size() > 5;
					}
					seriesObject.put("name", yaxis.getField().getName());
					switch (insQuota.getYaxisType()) {
					//折线图
					case "1":
						seriesObject.put("type", "line");
						seriesObject.put("barGap", "0");
						seriesObject.put("data", seriesData);
						seriesObject.put("symbol", "none");
						//设置辅助线
						if (null != yauxiliaryLineList && yauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(yauxiliaryLineList,yaxis.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
						}
						series.put(seriesObject);
						tooltip.put("trigger", "axis");
						break;
						//柱状图
					case "2":
						seriesObject.put("type", "bar");
						seriesObject.put("barGap", "0");
						seriesObject.put("data", seriesData);
						//设置辅助线
						if (null != yauxiliaryLineList && yauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(yauxiliaryLineList,yaxis.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
						}
						series.put(seriesObject);
						break;
						//堆积柱状图
					case "3":
						seriesObject.put("type", "bar");
						seriesObject.put("stack", "data");
						seriesObject.put("barGap", "0");
						seriesObject.put("data", seriesData);
						//设置辅助线
						if (null != yauxiliaryLineList && yauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(yauxiliaryLineList,yaxis.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
						}
						series.put(seriesObject);
						break;
						//条形图
					case "4":
						seriesObject.put("type", "bar");
						seriesObject.put("barGap", "0");
						seriesObject.put("data", seriesData);
						//设置辅助线
						if (null != yauxiliaryLineList && yauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(yauxiliaryLineList,yaxis.getField().getId(),"xAxis");
							seriesObject.put("markLine", markLine);
						}
						series.put(seriesObject);
						break;
					default:
						break;
					}
					
				}
			}
		}
		//次轴处理逻辑
		if (null != minorList && minorList.size() > 0) {
			for(int i = 0; i < minorList.size(); i++){
				InsYaxis minor = minorList.get(i);
				if ("0".equals(minor.getDelFlag())) {
					JSONObject seriesObject = new JSONObject();
					//如果按日期分组的话，日期SQL，默认取字段，可以选择单据创建日期
					String dateSQL = "c.`value`";
					if ("createDate".equals(insQuota.getXaxis().getId())) {
						dateSQL = "idd.create_date";
					}
					//选了年月日，已确定按date字段分组
					if (!"".equals(insQuota.getDateType()) && !"createDate".equals(insQuota.getXaxis().getId())) {
						dateSQL = "str_to_date(c.`value`,'%Y年%m月%d日 %H时%i分%s秒')";
					}
					List<InsCount> countList = dao.selectForCount(insQuota.getXaxis().getId(), minor.getField().getId(),minor.getAlgorithm(),insQuota.getDateType(),dateSQL,minor.getDigit());
					xaxisDataArray = new String[countList.size()];
					String[] seriesData = new String[countList.size()];
					for (int j = 0; j < countList.size(); j++) {
						InsCount insCount = countList.get(j);
						xaxisDataArray[j] = insCount.getXaxis();
						//最大值
						maxValue = (maxValue > Double.valueOf(insCount.getResult()))? maxValue:Double.valueOf(insCount.getResult());
						//无维度的情况下，x轴无意义
						if (null == insQuota.getXaxis() || StringUtils.isEmpty(insQuota.getXaxis().getId())) {
							xaxisDataArray[j]= "统计结果";
						}
						rotate = xaxisDataArray[j].length()>10 || countList.size() > 5;
						seriesData[j] = insCount.getResult();
					}
					seriesObject.put("name", minor.getField().getName());
					//seriesObject.put("label", new JSONObject().put("show", true));
					switch (insQuota.getMinorType()) {
					//折线图
					case "1":
						seriesObject.put("type", "line");
						seriesObject.put("symbol", "none");
						//设置辅助线
						if (null != mauxiliaryLineList && mauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(mauxiliaryLineList,minor.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
							seriesObject.put("symbol", "none");
						}
						tooltip.put("trigger", "axis");
						break;
						//柱状图
					case "2":
						seriesObject.put("type", "bar");
						//设置辅助线
						if (null != mauxiliaryLineList && mauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(mauxiliaryLineList,minor.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
						}
						break;
						//堆积柱状图
					case "3":
						seriesObject.put("type", "bar");
						seriesObject.put("stack", "data");
						//设置辅助线
						if (null != mauxiliaryLineList && mauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(mauxiliaryLineList,minor.getField().getId(),"yAxis");
							seriesObject.put("markLine", markLine);
						}
						break;
						//条形图
					case "4":
						seriesObject.put("type", "bar");
						//设置辅助线
						if (null != mauxiliaryLineList && mauxiliaryLineList.size() > 0) {
							JSONObject markLine = getMarkLine(mauxiliaryLineList,minor.getField().getId(),"xAxis");
							seriesObject.put("markLine", markLine);
						}
						break;
					default:
						break;
					}
					seriesObject.put("barGap", "0");
					seriesObject.put("data", seriesData);
					series.put(seriesObject);
				}
			}
		}
		//专门处理饼图
		if ("5".equals(insQuota.getYaxisType())) {
			//有维度
			if (null != insQuota.getXaxis() && !StringUtils.isEmpty(insQuota.getXaxis().getId())) {
				pieObject.put("data", pieDataArray);
				pieObject.put("type", "pie");
				pieObject.put("name", "统计结果");
				pieObject.put("radius", "55%");
				if (pieDataArray.length() > 0) {
					pieSeries.put(pieObject);
				}
			}else{
				pieObjectNoXaxis.put("data", pieDataArrayNoXasix);
				pieObjectNoXaxis.put("type", "pie");
				pieObjectNoXaxis.put("name", "统计结果");
				pieObjectNoXaxis.put("radius", "55%");
				if (pieDataArrayNoXasix.length() > 0) {
					pieSeries.put(pieObjectNoXaxis);
				}
			}
			//tooltip
			tooltip.put("trigger", "item");
			tooltip.put("formatter", "{a} <br/>{b} : {c} ({d}%)");
		}
		//专门处理计量图
		if ("6".equals(insQuota.getYaxisType())) {
			gaugeObject.put("data", gaugeDataArray);
			gaugeObject.put("type", "gauge");
			if ("1".equals(insQuota.getEnableSettingValue())) {
				double settingValue = getAuxiliaryValue(insQuota.getSettingValue());
				gaugeObject.put("max", settingValue);
			}
			if (gaugeDataArray.length() > 0) {
				gaugeSeries.put(gaugeObject);
			}
		}
		xaxis.put("data", xaxisDataArray);
		xaxis.put("type", "category");
		//x轴全部显示,根据情况倾斜
		JSONObject axisLabel = new JSONObject();
		axisLabel.put("interval", 0);
		axisLabel.put("rotate", -30);
		axisLabel.put("fontSize", 6);
		if (rotate) {
			xaxis.put("axisLabel", axisLabel);
		}
		//放入option
		option.put("title", title);
		option.put("tooltip", tooltip);
		option.put("legend", legend);
		//条形图逻辑
		if ("4".equals(insQuota.getYaxisType()) || "4".equals(insQuota.getMinorType())) {
			JSONObject yAxis = new JSONObject();
			String[] boundaryGap = new String[]{"20%","20%"};
			yAxis.put("boundaryGap", boundaryGap);
			option.put("xAxis", yAxis);
			option.put("yAxis", xaxis);
			//滚动条
			if (rotate) {
				JSONObject dataZoom = new JSONObject();
				dataZoom.put("type", "slider");
				dataZoom.put("yAxisIndex", 0);
				dataZoom.put("filterMode", "empty");
				option.put("dataZoom", dataZoom);
			}
		}else if(!"5".equals(insQuota.getYaxisType()) && !"6".equals(insQuota.getYaxisType())){
			JSONObject yAxis = new JSONObject();
			String[] boundaryGap = new String[]{"20%","20%"};
			yAxis.put("boundaryGap", boundaryGap);
			option.put("xAxis", xaxis);
			option.put("yAxis", yAxis);
			//滚动条
			if (rotate) {
				JSONObject dataZoom = new JSONObject();
				dataZoom.put("type", "slider");
				dataZoom.put("xAxisIndex", 0);
				dataZoom.put("filterMode", "empty");
				option.put("dataZoom", dataZoom);
			}
		//饼图、计量图、指标卡不用放x轴和y轴
		}else {
			
		}
		//
		if ("5".equals(insQuota.getYaxisType())) {
			option.put("series", pieSeries);
		}else if ("6".equals(insQuota.getYaxisType())) {
			option.put("series", gaugeSeries);
		}else {
			option.put("series", series);
		}
		return option;
	}
	
	/**
	 * 根据辅助线规则获取最终值，可以用于辅助线和设定值
	 * @param insAuxiliaryLine
	 * @return
	 */
	private double getAuxiliaryValue(InsAuxiliaryLine insAuxiliaryLine){
		double result = 0.0;
		if (StringUtils.isEmpty(insAuxiliaryLine.getField().getId()) 
				|| StringUtils.isEmpty(insAuxiliaryLine.getValueType())
				|| StringUtils.isEmpty(insAuxiliaryLine.getDynamicValue())) {
			return result;
		}
		//固定值，直接返回
		if ("1".equals(insAuxiliaryLine.getValueType())) {
			result = Double.valueOf(StringUtils.isEmpty(insAuxiliaryLine.getFinalValue())?"0":insAuxiliaryLine.getFinalValue());
			return result;
		}else {
			//得到统计源数组
			List<Long> values = insDataLinkDao.getByFieldId(insAuxiliaryLine.getField().getId());
			
			if (values.size() <= 0) {
				return result;
			}
			double[] arr = new double[values.size()];
			for (int i = 0;i <values.size();i++) {
				arr[i] = values.get(i).doubleValue();
			}
			String method = insAuxiliaryLine.getDynamicValue();
			/**
			 *  <option value="1">最大值</option>
				<option value="2">最小值</option>
				<option value="3">平均值</option>
				<option value="4">求和</option>
				<option value="5">众数</option>
				<option value="6">中位数</option>
				<option value="7">中列数</option>
				<option value="8">四分位数</option>
				<option value="9">极差</option>
				<option value="10">四分位数极差</option>
				<option value="11">方差</option>
				<option value="12">绝对平均偏差</option>
				<option value="13">中位数绝对偏差</option>
				<option value="14">标准差</option>
			 */
			switch (method) {
			case "1":
				result = MathUtil.getMax(arr);
				break;
			case "2":
				result = MathUtil.getMin(arr);
				break;
			case "3":
				result = MathUtil.getMean(arr);
				break;
			case "4":
				result = MathUtil.getSum(arr);
				break;
			case "5":
				result = MathUtil.getMode(arr);
				break;
			case "6":
				result = MathUtil.getMedian(arr);
				break;
			case "7":
				result = MathUtil.getMidrange(arr);
				break;
			case "9":
				result = MathUtil.getRange(arr);
				break;
			case "10":
				result = MathUtil.getQuartilesRange(arr);
				break;
			case "11":
				result = MathUtil.getVariance(arr);
				break;
			case "12":
				result = MathUtil.getAbsoluteAverageDeviation(arr);
				break;
			case "13":
				result = MathUtil.getMedianAbsoluteDeviation(arr);
				break;
			case "14":
				result = MathUtil.getStandardDevition(arr);
				break;

			default:
				break;
			}
		}
		
		return result;
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false, rollbackFor = Exception.class)
	public void updateEnableSettingValue(InsQuota insQuota){
		dao.updateEnableSettingValue(insQuota);
	}
	
	/**
	 * 画辅助线方法
	 * @param auxiliaryLineList
	 * @return
	 */
	private JSONObject getMarkLine(List<InsAuxiliaryLine> auxiliaryLineList,String fieldId,String valueName){
		JSONObject markLine = new JSONObject();
		markLine.put("symbol", "none");
		JSONArray data = new JSONArray();
		for (InsAuxiliaryLine insAuxiliaryLine : auxiliaryLineList) {
			if ("0".equals(insAuxiliaryLine.getDelFlag()) && fieldId.equals(insAuxiliaryLine.getField().getId())) {
				double value = getAuxiliaryValue(insAuxiliaryLine);
				JSONObject line = new JSONObject();
				//算法
				String dynamicValue = insAuxiliaryLine.getDynamicValue();
				//固定值还是计算值
				String valueType = insAuxiliaryLine.getValueType();
				//固定值
				if ("1".equals(valueType)) {
					line.put("name", "固定值");
				}else {
					String method = "求和";
					switch (dynamicValue) {
					case "1":
						method = "最大值";
						break;
					case "2":
						method = "最小值";
						break;
					case "3":
						method = "平均值";
						break;
					case "4":
						method = "求和";
						break;
					case "5":
						method = "众数";
						break;
					case "6":
						method = "中位数";
						break;
					case "7":
						method = "中列数";
						break;
					case "9":
						method = "极差";
						break;
					case "10":
						method = "四分位数极差";
						break;
					case "11":
						method = "方差";
						break;
					case "12":
						method = "绝对平均偏差";
						break;
					case "13":
						method = "中位数绝对偏差";
						break;
					case "14":
						method = "标准差";
						break;
						
					default:
						break;
					}
					line.put("name", method);
				}
				line.put(valueName, value);
				data.put(line);
			}
		}
		markLine.put("data", data);
		return markLine;
	}
}